package DBLayer;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import ModelLayer.Invoice;

public class DbInvoice implements IfDbInvoice
{
	private Connection con;
	
	public DbInvoice()
	{
		con = DbConnection.getInstance().getDBcon();
	}

	public ArrayList<Invoice> getAllInvoices(boolean retriveAssociation) 
	{
        return miscWhere("", retriveAssociation);
	}

	private ArrayList<Invoice> miscWhere(String wClause, boolean retrieveAssociation)
	{
        ResultSet results;
	    ArrayList<Invoice> list = new ArrayList<Invoice>();	
		
	    String query =  buildQuery(wClause);
  
        try
        {
        	Statement stmt = con.createStatement();
        	stmt.setQueryTimeout(5);
        	results = stmt.executeQuery(query);
	 	
        	while( results.next() )
        	{
        		Invoice invoiceObject = new Invoice();
        		invoiceObject = buildInvoice(results);	
                list.add(invoiceObject);	
        	}
            stmt.close();     
		}
	 	catch(Exception e){
	 		System.out.println("Query exception - select: "+e);
			e.printStackTrace();
	 	}
		return list;
	}

	private Invoice singleWhere(String wClause, boolean retrieveAssociation)
	{
		ResultSet results;
		Invoice invoiceObject = new Invoice();
                
	        String query =  buildQuery(wClause);
                System.out.println(query);
		try
		{ 
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 		results = stmt.executeQuery(query);
	 		
	 		if( results.next() )
	 		{
                            invoiceObject = buildInvoice(results);
                            stmt.close();
			}
                        else
                        {
                            invoiceObject = null;
                        }
		}	
	 	catch(Exception e){
	 		System.out.println("Query exception: "+e);
	 	}
		return invoiceObject;
	}
	
	private String buildQuery(String wClause)
	{
	    String query="SELECT paymentDate FROM Invoice";
		
		if (wClause.length()>0)
		{
			query=query+" WHERE "+ wClause;
		}			
		return query;
	}

	private Invoice buildInvoice(ResultSet results)
    {   
		Invoice invoiceObject = new Invoice();
        try
        {
            invoiceObject.setPaymentDate(results.getDate("paymentDate"));
        }
        catch(Exception e)
        {
            System.out.println("There is a failure in building the Invoice");
        }
        return invoiceObject;
    }
	
	public Invoice findInvoicesByPaymentDate(Date paymentDate, boolean retriveAssociation) 
	{
		String wClause = "PaymentDate = '" + paymentDate + "'";
        System.out.println("SearchInvoice " + wClause);
        return singleWhere(wClause, retriveAssociation);
	}

	public int insertInvoice(Invoice invoice) throws Exception 
	{
		int rc = -1;
		String query="INSERT INTO Invoice(paymentDate) VALUES('"+
		  		invoice.getPaymentDate()  + "')";
		try
		{ 
	          Statement stmt = con.createStatement();
	          stmt.setQueryTimeout(5);
	     	  rc = stmt.executeUpdate(query);
	          stmt.close();
	    }
	    catch(SQLException ex)
	    {
	        System.out.println("Invoice is not created");
	        throw new Exception ("Invoice is not inserted correct");
	    }
	    return(rc);
	}

	public int updateInvoice(Invoice Invoice) 
	{
		Invoice InvoiceObject  = Invoice;
		int lol = -1;

		String query="UPDATE Invoice SET "+
		 	  "PaymentDate ='"+ InvoiceObject.getPaymentDate()+ "', " +
		      " WHERE PaymentDate = '"+ InvoiceObject.getPaymentDate() + "'";
              System.out.println("Update query:" + query);
  		try
  		{
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 	 	lol  = stmt.executeUpdate(query);

	 	 	stmt.close();
		}
	 	catch(Exception ex)
	 	{
	 	 	System.out.println("Update exception in Invoice db: " + ex);
	  	}
		return(lol);
	}

	public int deleteInvoice(Date paymentDate)
	{
        int lol = -1;

        String query="DELETE FROM Invoice WHERE PaymentDate = '"+ paymentDate +"'";
        System.out.println(query);
	  	try
	  	{
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 	  	lol = stmt.executeUpdate(query);
	 	  	stmt.close();
  		}
   	    catch(Exception ex)
   	    {
	 	  	System.out.println("Delete exception in Invoice db: "+ex);
   	    }
		return(lol);
	}
}
